/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.DAO;

import Model.Pojo.MenuProduct;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author luong
 */
public class MenuProductImp implements MenuProductDAO{

    @Override
    public boolean AjouterMenuProduct(MenuProduct menu) {
        boolean Resultat;
        return Resultat=Model.DAO.ModelDaoLibrarie.Ajouter(menu);
    }

    @Override
    public boolean SuprimerMenuProduct(MenuProduct menu) {
        boolean Resultat=false;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {           
            String hql= String.format("select m from MenuProduct m where m.NameMenuProduct='%s'",menu.getNameMenuProduct());
            menu= (MenuProduct) session.createQuery(hql).uniqueResult();
            session.delete(menu);
            session.beginTransaction().commit();
            Resultat=true;
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return Resultat;
    }

    @Override
    public boolean UpdateMenuProduct(MenuProduct menu) {
        boolean Resultat;
        return Resultat=Model.DAO.ModelDaoLibrarie.Update(menu);
    }

    @Override
    public List<MenuProduct> GetListMenuProduct() {
       List<MenuProduct> listMenuProduct = null; 
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {
            String hql="from MenuProduct";
            Query query= session.createQuery(hql);
            listMenuProduct=query.list(); 
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return listMenuProduct; 
    }
    
}
